//
//  DiaryOCTableViewCell.m
//  MoodDiary
//
//  Created by MoodDiary on 2020/12/7.
//

#import "DiaryOCTableViewCell.h"

#import "DiaryOCCollectionViewCell.h"

@implementation DiaryOCTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DiaryOCCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DiaryOCCollectionViewCell"];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 2;
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:15],
                           NSParagraphStyleAttributeName:paragraphStyle
    };
    _contentTextView.typingAttributes = dict;
    
}

- (IBAction)action:(id)sender {
    [self.delegate diaryOCTableViewCellSelectIndex:self.indexPath];
}

- (void)setArray:(NSArray *)array {
    _array = array;
    
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    UILabel *desLabel = [[UILabel alloc] init];
    desLabel.numberOfLines = 0;
    UIFont *font = [UIFont systemFontOfSize:12];
    desLabel.text = self.array[indexPath.row];

    CGSize size = CGSizeMake(120, 500);//跟label的宽设置一样
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
    size =[desLabel.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    
    
    return CGSizeMake(self.collectionView.height + size.width+30, self.collectionView.height);
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    DiaryOCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiaryOCCollectionViewCell" forIndexPath:indexPath];
  
    cell.label.text = self.array[indexPath.row];
//    cell.imgV.image = [UIImage imageNamed:cell.label.text];
    
    UIImage *showImg = [UIImage imageNamed:cell.label.text];
    if (showImg) {
        cell.imgV.image = showImg;
    } else {
        NSMutableArray *activtyArr = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"KUSERActiveImagesArrName"]];
        
        NSArray *deletedImgArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"KUSERActiveDeletedImagesArrName"]];
        [activtyArr addObjectsFromArray:deletedImgArray];
        
        for (NSDictionary *cacheDict in activtyArr) {
            
            if ([cell.label.text isEqualToString:cacheDict[@"text"]]) {
                cell.imgV.image = [UIImage imageNamed: cacheDict[@"imgName"]];
                break;
            }
        }
    }
    
    if (indexPath.row == self.array.count - 1) {
        cell.pointView.hidden = YES;
    } else {
        cell.pointView.hidden = NO;
    }
    
    return cell;
}

@end
